package com.joelaws.prototype.server.blackberry;

import org.restlet.Context;
import org.restlet.data.MediaType;
import org.restlet.data.Request;
import org.restlet.data.Response;
import org.restlet.resource.Representation;
import org.restlet.resource.Resource;
import org.restlet.resource.StringRepresentation;
import org.restlet.resource.Variant;

import com.joelaws.prototype.client.data.User;
import com.joelaws.prototype.server.dao.DAOFactory;

public class UserInfo extends Resource {

	private long userID;

	public UserInfo(Context context, Request request, Response response) {
		super(context, request, response);

		String[] data = request.getResourceRef().getRemainingPart().split("=");
		if (data != null && data.length == 2)
			userID = Long.parseLong(data[1]);
		else
			userID = Long.MIN_VALUE;
		super.getVariants().add(new Variant(MediaType.TEXT_PLAIN));
	}

	@Override
	public Representation getRepresentation(Variant variant) {
		if (userID == Long.MIN_VALUE)
			return new StringRepresentation("Invalid userID",
					MediaType.TEXT_PLAIN);

		User user = DAOFactory.getUserDAO().getUser(userID);

		StringBuilder builder = new StringBuilder();
		builder.append(user.getFirstName() + " " + user.getLastName());
		builder.append(";");
		builder.append(user.getDatingDetails().getEyeColor());
		builder.append(";");
		builder.append(user.getDatingDetails().getHairColor());
		builder.append(";");
		builder.append(user.getNetworkingDetails().getDegree());
		builder.append(";");
		builder.append(user.getNetworkingDetails().getEmployment());
		builder.append(";");
		builder.append(user.getNetworkingDetails().getIncome());
		builder.append(";");
		builder.append(user.getNetworkingDetails().getReligion());

		return new StringRepresentation(builder.toString(),
				MediaType.TEXT_PLAIN);
	}

	public static String getURL() {
		return "/getUserInfo";
	}
}
